package example.guice.web

import com.google.inject.servlet.GuiceServletContextListener
import com.google.inject.Guice.createInjector
import com.google.inject.Injector
import dba.example.guice.module.ExampleModule
import grizzled.slf4j.Logging
import java.util.Properties

class ExampleGuiceServletConfig extends GuiceServletContextListener with Logging {

  override protected def getInjector(): Injector = {
    info("Creating injector")
    
    val jpaProps = loadProperties("jpa.properties")
    
    createInjector(new ExampleModule(jpaProps), new ExampleServletModule());
  }

  private def loadProperties(name: String) = {
    val props = new Properties()
    try {
      val is = getClass.getClassLoader.getResourceAsStream(name)
      props.load(is)
      is.close()
    } catch {
      case e: Exception => error(s"Error loading properties file: $name", e);
    }
    props
  }

}